home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-02-24 | 7.1 KB | 253 lines | [TEXT/PJMM] |
- unit MyPrinting;
-
- { This code is part of the Finger/Fingerd source code, written in THINK Pascal 4 }
- { Copyright 1991-1992 Peter N Lewis }
- { If you use this code, you must give me credit in your about box and documentation }
- { This is part of my generic library of routines }
-
- interface
-
- uses
- Printing;
-
- type
- PObject = object
- procedure Create;
- procedure Destroy;
- function CountPages (r: rect): integer;
- procedure DrawPage (r: rect; gp: GrafPtr; pg: integer; first, last: boolean);
- procedure OpenPrintingStatusDialog;
- procedure DoIdle;
- procedure ClosePrintingStatusDialog;
- procedure PostPrintingErrors (oe: OSErr);
- end;
-
- var
- thePrintingRecordHandle: THPrint;
-
- procedure InitPrinting;
- procedure FinishPrinting;
- procedure PrintStuff (pob: PObject; thePrRecHdl: THPrint);
- procedure DoPageSetup (pob: PObject; thePrRecHdl: THPrint);
- procedure SegmentPrinting;
-
- implementation
-
- uses
- BaseGlobals;
-
- {$S Printing}
- procedure SegmentPrinting;
- begin
- end;
-
- {$S Printing}
- procedure PObject.Create;
- begin
- end;
-
- {$S Printing}
- procedure PObject.Destroy;
- begin
- dispose(self);
- end;
-
- {$S Printing}
- function PObject.CountPages (r: rect): integer;
- begin
- CountPages := 1;
- end;
-
- {$S Printing}
- procedure PObject.DoIdle;
- begin
- end;
-
- {$S Printing}
- procedure PObject.DrawPage (r: rect; gp: GrafPtr; pg: integer; first, last: boolean);
- begin
- SetPort(gp);
- with r do
- MoveTo((left + right) div 2 - 20, (top + bottom) div 2);
- DrawString('Not Yet Implemented');
- end;
-
- {$S Printing}
- procedure PObject.PostPrintingErrors (oe: OSErr);
- var
- s: str255;
- a: integer;
- begin
- NumToString(oe, s);
- ParamText('Print Error = ', s, '', '');
- a := Alert(fail_alert_id, nil);
- end;
-
- {$S Printing}
- procedure PObject.OpenPrintingStatusDialog;
- begin
- SetCursor(GetCursor(watchCursor)^^);
- end;
-
- {$S Printing}
- procedure PObject.ClosePrintingStatusDialog;
- begin
- SetCursor(arrow);
- end;
-
- var
- gpob: PObject;
-
- {$S}
- procedure DoIdle;
- begin
- gpob.DoIdle;
- end;
-
- {$S Init}
- procedure InitPrinting;
- begin
- thePrintingRecordHandle := THPrint(NewHandle(SIZEOF(TPrint)));
- PrOpen;
- if PrError = noErr then begin
- PrintDefault(thePrintingRecordHandle);
- PrClose;
- end;
- end;
-
- {$S Term}
- procedure FinishPrinting;
- begin
- DisposHandle(handle(thePrintingRecordHandle));
- end;
-
- {$S}
- procedure DoPageSetup (pob: PObject; thePrRecHdl: THPrint);
- var
- dummy: boolean;
- begin
- PrOpen;
- if PrError = noErr then
- dummy := PrStlDialog(thePrRecHdl)
- else
- pob.PostPrintingErrors(PrError);
- PrClose;
- end;
-
- {*------ PrintStuff ---------------------------------------------------------*}
- {** ** PrintStuff will call all of the necessary Print Manager calls to print }
- {** a document. It checks PrError() after each Print Manager call. If an }
- { ** error is found, all of the Print Manager open calls (i.e., PrOpen, }
- { ** PrOpenDoc...) will have a corresponding close call before the error }
- { ** is posted to the user. You want to use this approach to make sure the }
- { ** Print Manager closes properly and all temporary memory is released. }
- {$S Printing}
- procedure PrintStuff (pob: PObject; thePrRecHdl: THPrint);
-
- var
- copies, firstPage, lastPage, loop, numberOfCopies, pageNumber, printmgrsResFile, realNumberOfPagesInDoc: Integer;
- PrintError: LongInt;
- oldPort: GrafPtr;
- thePrPort: TPPrPort;
- theStatus: TPrStatus;
-
- begin
- GetPort(oldPort);
- gpob := pob;
-
- PrOpen;
- if PrError = noErr then begin
- { Save the current resource file (i.e. the printer driver's) so the driver will not lose its }
- { resources upon return from the pIdleProc.}
- printmgrsResFile := CurResFile;
-
- realNumberOfPagesinDoc := pob.CountPages(thePrRecHdl^^.prInfo.rPage);
-
- if PrJobDialog(thePrRecHdl) then begin
- { Get the number of copies of the document that}
- { the user wants printed from iCopies of the TPrJob}
- { record (IM II-151).}
-
- numberOfCopies := thePrRecHdl^^.prJob.iCopies;
-
- { Get the first and last pages of the document that}
- { were requested to be printed by the user from}
- { iFstPage and iLastPage from the TPrJob record}
- { (IM II-151).}
-
- firstPage := thePrRecHdl^^.prJob.iFstPage;
- lastPage := thePrRecHdl^^.prJob.iLstPage;
-
- { Print "all" pages in the print loop}
-
- thePrRecHdl^^.prJob.iFstPage := 1;
- thePrRecHdl^^.prJob.iLstPage := 9999;
- if (realNumberOfPagesinDoc < lastPage) then
- lastPage := realNumberOfPagesinDoc;
-
- { Print the number of copies of the document}
- { requested by the user from the Print Job Dialog.}
- pob.OpenPrintingStatusDialog;
-
- for copies := 1 to numberOfCopies do begin
- { Install and call your "Print Status Dialog".}
- thePrRecHdl^^.prJob.pIdleProc := @DoIdle;
-
- UseResFile(printmgrsResFile);
-
- thePrPort := PrOpenDoc(thePrRecHdl, nil, nil);
-
- if (PrError = noErr) then begin
- { Print the range of pages of the document requested by the user from the Print Job Dialog.}
-
- pageNumber := firstPage;
- while ((pageNumber <= lastPage) and (PrError = noErr)) do begin
-
- PrOpenPage(thePrPort, nil);
-
- if (PrError = noErr) then begin
- { rPage (IM II-150) is the printable area for the currently selected printer. By passing the current}
- { enables your app to use the same routine to draw to the screen and the printer's GrafPort.}
-
- pob.DrawPage(thePrRecHdl^^.prInfo.rPage, GrafPtr(thePrPort), pageNumber, firstPage = pageNumber, lastPage = pageNumber);
- end;
- PrClosePage(thePrPort);
- pageNumber := pageNumber + 1;
- end; {** End pagenumber loop **}
- end;
- PrCloseDoc(thePrPort);
- end; {** End copies loop **}
-
- pob.ClosePrintingStatusDialog;
- { The printing job is being canceled by the request}
- { of the user from the Print Style Dialog or the}
- { Print Job Dialog PrError will be set to iPrAbort}
- { to tell the Print Manager to abort the current}
- { printing job.}
- end
- else
- PrSetError(iPrAbort); {** Cancel from the job dialog **}
- end;
- if (thePrRecHdl^^.prJob.bJDocLoop = bSpoolLoop) and (PrError = noErr) then
- PrPicFile(thePrRecHdl, nil, nil, nil, theStatus);
-
- { Grab the printing error before you close}
- { the Print Manager and the error disappears.}
-
- PrintError := PrError;
-
- PrClose;
-
- { You do not want to report any printing errors until you have fallen}
- { through the printing loop. This will make sure that ALL of the Print}
- { Manager's open calls have their corresponding close calls, thereby}
- { enabling the Print Manager to close properly and that all temporary}
- { memory allocations are released.}
-
- if (PrintError <> noErr) and (PrintError <> iPrAbort) then
- pob.PostPrintingErrors(PrintError);
-
- SetPort(oldPort);
- end; {** PrintStuff **}
-
- end.